<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/11
 */

namespace App\Http\Controllers;

use App\Filters\AdsSpaceFilter;
use App\Http\Requests\AdsSpaceRequest;
use App\Http\Resources\AdsSpaceCollection;
use App\Http\Resources\AdsSpaceResource;
use App\Models\AdsSpace;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 广告位模块
 * Class AdsSpaceController
 * @package App\Http\Controllers
 */
class AdsSpaceController extends Controller
{

    /**
     * 列表
     * @param AdsSpaceFilter $filter
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(AdsSpaceFilter $filter)
    {
        $paginate = AdsSpace::filter($filter)->paginate($this->pageSize());
        $result = AdsSpaceCollection::make($paginate)->toArray(null);
        return $this->success('', $result);
    }

    /**
     * 添加
     * @param AdsSpaceRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(AdsSpaceRequest $request)
    {
        $data = $request->validate('store');
        $adsSpace = new AdsSpace($data);
        if (!$adsSpace->save()) return $this->errorStored();
        return $this->successStored();
    }

    /**
     * 查看
     * @param AdsSpace $ads_space
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(AdsSpace $ads_space)
    {
        $adsSpace = AdsSpaceResource::make($ads_space)->except(['created_at', 'updated_at', 'info_category_name'])->toArray(null);
        return $this->success('', $adsSpace);
    }

    /**
     * 修改
     * @param AdsSpaceRequest $request
     * @param AdsSpace $ads_space
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function update(AdsSpaceRequest $request, AdsSpace $ads_space)
    {
        $data = $request->validate('update');
        if (!$ads_space->update($data)) return $this->errorUpdated();
        return $this->successUpdated();
    }

    /**
     * 删除
     * @param $id // 1或者1,2
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id)
    {
        DB::beginTransaction();
        try {
            $between = explode(',', $id);
            AdsSpace::destroy($between);
            DB::commit();
            return $this->successDestoryed();
        } catch (\Exception $e) {
            DB::rollback();
            return $this->exception($e->getMessage())->errorDestoryed();
        }
    }

    /**
     * 更新状态
     * @param AdsSpaceRequest $request
     * @param AdsSpace $ads_space
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function updateStatus(AdsSpaceRequest $request, AdsSpace $ads_space)
    {
        $data = $request->validate('updateStatus');
        if (!$ads_space->update($data)) return $this->errorUpdated();
        return $this->successUpdated();
    }

    /**
     * 获取广告模块所需广告位配置信息
     * @param AdsSpace $ads_space
     * @return \Illuminate\Http\JsonResponse
     */
    public function adsSpaceConfig(AdsSpace $ads_space)
    {
        $adsSpace = AdsSpace::select(['id', 'name', 'type', 'width', 'height'])->find($ads_space->id);
        return $this->success('', $adsSpace->toArray());
    }
}
